package models

import (
	"time"

	"gorm.io/gorm"
)

// User 用户中心表
type User struct {
	UserId int `gorm:"primary_key" json:"user_id"` // ID

	UserName      string `json:"user_name"`      // 用户名称
	AccessToken   string `json:"access_token"`   // 授权TOKEN
	RefreshToken  string `json:"refresh_token"`  // 刷新TOKEN
	Balance       int    `json:"balance"`        // 算力
	ExpiresIn     int    `json:"expires_in"`     // 过期时间
	AliId         int    `json:"ali_id"`         // 阿里ID
	MemberId      string `json:"member_id"`      // 用户ID
	ResourceOwner string `json:"resource_owner"` // 店铺名称

	CreateTime time.Time `json:"create_time"` // 创建时间
	UpdateTime time.Time `json:"update_time"` // 更新时间
}

// TableName 表名称
func (*User) TableName() string {
	return "user"
}

// GetUser 查询多条数据
func GetUser(fields []string, maps map[string]interface{}) ([]*User, error) {
	var address []*User
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&address).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return address, nil
}

func GetUserPage(fields []string, maps map[string]interface{}, pageNum int, pageSize int, order string) ([]*User, error) {
	var User []*User
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Offset(pageNum).Limit(pageSize).Order(order).Find(&User).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return User, nil
}

// GetUserInfo 查询多条数据
func GetUserInfo(fields []string, maps map[string]interface{}) (*User, error) {
	var User User
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&User).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &User, nil
}

// AddUser 插入数据
func AddUser(data map[string]interface{}) (int, error) {
	address := User{
		UserName:      data["user_name"].(string),
		AccessToken:   data["access_token"].(string),
		RefreshToken:  data["refresh_token"].(string),
		ExpiresIn:     data["expires_in"].(int),
		AliId:         data["ali_id"].(int),
		MemberId:      data["member_id"].(string),
		ResourceOwner: data["resource_owner"].(string),
	}
	result := db.Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.UserId, nil
}

// AddUser 插入数据
func AddUserDB(data map[string]interface{}, db *gorm.DB) (int, error) {
	address := User{
		UserName:      data["user_name"].(string),
		AccessToken:   data["access_token"].(string),
		RefreshToken:  data["refresh_token"].(string),
		Balance:       data["balance"].(int),
		ExpiresIn:     data["expires_in"].(int),
		AliId:         data["ali_id"].(int),
		MemberId:      data["member_id"].(string),
		ResourceOwner: data["resource_owner"].(string),
	}
	result := db.Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.UserId, nil
}

// EditUser modify a single User
func EditUser(where map[string]interface{}, data interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&User{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

func GetUserTotal(where map[string]interface{}) (int64, error) {
	var count int64
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return 0, errs
	}
	if err := db.Model(&User{}).Where(cond, vals...).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func CleanUserDB(where map[string]interface{}, db *gorm.DB) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&User{}).Error; err != nil {
		return err
	}

	return nil
}

func CleanUser(where map[string]interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&User{}).Error; err != nil {
		return err
	}

	return nil
}
